package cn.mayaslight.common.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;

public class DataFormat {
	
	private static final Logger log = Logger.getLogger(DataFormat.class);

	public static String getKey(String[] keyArray, String key) {
		if (keyArray != null) {
			if (keyArray.length > 0) {
				return keyArray[0];
			}
			return key;
		}
		return key;
	}

	public static String getStringKey(Map map, String key) {
		if (map != null) {
			Object obj = map.get(key);
			if (obj != null) {
				if (((obj instanceof String[])) && (((String[]) (String[]) obj).length > 1)) {
					StringBuffer sbf = new StringBuffer();
					for (int i = 0; i < ((String[]) (String[]) obj).length; i++) {
						sbf.append(((String[]) (String[]) obj)[i]);
						if (i < ((String[]) (String[]) obj).length - 1) {
							sbf.append(",");
						}
					}
					return sbf.toString();
				}
				if (((obj instanceof String[])) && (((String[]) (String[]) obj).length > 0)) {
					if ("null".equals(((String[]) (String[]) obj)[0])) {
						return "";
					}
					return ((String[]) (String[]) obj)[0];
				}
				if ((obj instanceof String)) {
					return (String) obj;
				}
				return obj.toString();
			}
		}

		return "";
	}

	public static long getLongKey(Map map, String key) {
		return getLongKey(getStringKey(map, key));
	}

	public static int getIntKey(Map map, String key) {
		return getIntKey(getStringKey(map, key));
	}

	public static short getShortKey(Map map, String key) {
		return stringToShort(getStringKey(map, key));
	}

	public static short getShortKey(String key) {
		return Short.parseShort(key);
	}

	public static boolean getBooleanKey(String key) {
		if ("1".equals(key)) {
			return true;
		}
		return Boolean.parseBoolean(key);
	}

	public static boolean getBooleanKey(Map map, String key) {
		String s = getStringKey(map, key);

		return (s != null) && ((s.equalsIgnoreCase("true")) || (s.equalsIgnoreCase("1")));
	}

	public static Date getDateKey(Map map, String key) throws Exception {
		return stringToDate(getStringKey(map, key));
	}

	public static Date getDateTimeKey(Map map, String key) throws Exception {
		return stringToDateTime(getStringKey(map, key));
	}

	public static long getLongKey(String key) {
		long keyId = -1L;
		try {
			keyId = Long.parseLong(key);
		} catch (NumberFormatException ex) {
			if ((null != key) && (!"".equals(key.trim()))) {
				log.warn("DataFormat.getLongKey方法错误提示：该关键字[" + key + "]不是长整数类型!");
			}
		}
		return keyId;
	}

	public static int getIntKey(String key) {
		int keyId = -1;
		try {
			keyId = Integer.parseInt(key);
		} catch (NumberFormatException ex) {
			if ((null != key) && (!"".equals(key.trim()))) {
				log.warn("DataFormat.getIntKey方法错误提示：该关键字[" + key + "]不是整数类型!");
			}
		}
		return keyId;
	}

	public static int getIntKey(Object obj) {
		return getIntKey((String) obj);
	}

	public static short getShortKey(Object obj) {
		return getShortKey((String) obj);
	}

	public static long getLongKey(Object obj) {
		return getLongKey((String) obj);
	}

	public static boolean getBooleanKey(Object obj) {
		return getBooleanKey((String) obj);
	}

	public static Date stringToDate(String key) throws Exception {
		Date date = null;
		try {
			date = DateUtil.formatStringToDate(key);
		} catch (ParseException ex) {
			if ((null != key) && (!"".equals(key.trim()))) {
				throw new Exception("DataFormat.stringToDate方法错误提示：该关键字[" + key + "]不是日期YYYY-MM-DD类型!");
			}
		}

		return date;
	}

	public static Date stringToDateTime(String key) throws Exception {
		Date date = null;
		try {
			date = DateUtil.formatStringToDate(key, DateUtil.DATETIME_FORMAT);
		} catch (ParseException ex) {
			if ((null != key) && (!"".equals(key.trim()))) {
				throw new Exception("DataFormat.stringToDateTime方法错误提示：该关键字[" + key + "]不是日期YYYY-MM-DD HH:mm:ss类型!");
			}
		}

		return date;
	}

	public static short stringToPositiveShort(String data) {
		short result = stringToShort(data);
		if (result < 0)
			result = 0;
		return result;
	}

	public static short stringToShort(String data) {
		short ret = -1;
		try {
			ret = Short.parseShort(data);
		} catch (NumberFormatException ex) {
			if ((null != data) && (!"".equals(data.trim()))) {
				log.warn("DataFormat.stringToShort方法错误提示：该关键字[" + data + "]不是短整数类型!");
			}
		}
		return ret;
	}

	public static int stringToPositiveInt(String data) {
		int result = stringToInt(data);
		if (result < 0)
			result = 0;
		return result;
	}

	public static int stringToInt(String data) {
		return getIntKey(data);
	}

	public static long stringToPositiveLong(String data) {
		long result = stringToLong(data);
		if (result < 0L)
			result = 0L;
		return result;
	}

	public static long stringToLong(String data) {
		return getLongKey(data);
	}

	public static double stringToPositiveDouble(String data) {
		double result = stringToDouble(data);
		if (result < 0.0D)
			result = 0.0D;
		return result;
	}

	public static double stringToDouble(String data) {
		double ret = -1.0D;
		try {
			ret = Double.parseDouble(data);
		} catch (NumberFormatException ex) {
			String errMsg = "该字段不是double类型!" + ex.getMessage();
			log.warn(errMsg);
		}
		return ret;
	}

	public static double stringToPositiveFloat(String data) {
		float result = stringToFloat(data);
		if (result < 0.0F)
			result = 0.0F;
		return result;
	}

	public static float stringToFloat(String data) {
		float ret = -1.0F;
		try {
			ret = Float.parseFloat(data);
		} catch (NumberFormatException ex) {
			String errMsg = "该字段不是float类型!" + ex.getMessage();
			log.warn(errMsg);
		}
		return ret;
	}

	public static char stringToChar(String data) {
		char ret = ' ';
		if (data == null)
			return ret;
		try {
			ret = data.charAt(0);
		} catch (NumberFormatException ex) {
			if ((null != data) && (!"".equals(data.trim()))) {
				log.warn("DataFormat.stringToChar方法错误提示：该关键字[" + data + "]不是char类型!");
			}
		}
		return ret;
	}

	public static String getPositiveString(long value) {
		if (value <= 0L) {
			return "";
		}
		return longToString(value);
	}

	public static String getNonNegativeString(long value) {
		if (value < 0L) {
			return "";
		}
		return longToString(value);
	}

	public static String intToString(int value) {
		return Integer.toString(value);
	}

	public static String shortToString(short value) {
		return Short.toString(value);
	}

	public static String longToString(long value) {
		return Long.toString(value);
	}

	public static String charToString(char c) {
		return Character.toString(c);
	}

	public static String floatToString(float value) {
		return Float.toString(value);
	}

	public static String doubleToString(double value) {
		NumberFormat format = NumberFormat.getNumberInstance();
		return format.format(value);
	}

	public static String currencyToPlainText(long lMoney) {
		String strResult = "";
		long lTempMoney = lMoney;
		if (lMoney < 0L) {
			lTempMoney = -lMoney;
		}
		String yuan = Long.toString(lTempMoney / 100L);
		long left = lTempMoney % 100L;
		String jiao = Long.toString(left / 10L);
		left %= 10L;
		strResult = yuan + "." + jiao + left;

		if (lMoney < 0L) {
			strResult = "-" + strResult;
		}

		return strResult;
	}

	public static long stringToCurrency(String strValue) {
		long lMoney = -1L;
		try {
			BigDecimal t = new BigDecimal(strValue);
			t = t.movePointRight(2);
			lMoney = t.longValue();
		} catch (Exception ex) {
			log.warn("stringToCurrency exception: " + ex.getMessage());
		}

		return lMoney;
	}

	public static boolean stringToBoolean(String str) {
		return Boolean.parseBoolean(str);
	}

	public static String changeCharset(String str, String newCharset) throws UnsupportedEncodingException {
		if (str != null) {
			byte[] bs = str.getBytes();

			return new String(bs, newCharset);
		}
		return null;
	}

	public static String changeCharset(String str, String oldCharset, String newCharset) throws UnsupportedEncodingException {
		if (str != null) {
			byte[] bs = str.getBytes(oldCharset);

			return new String(bs, newCharset);
		}
		return null;
	}
}
